# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Andrew Smeltzov <andrew.smeltzov@gmail.com>

_realname=pyside2
pkgbase=mingw-w64-${_realname}
pkgname=(${MINGW_PACKAGE_PREFIX}-shiboken2
         ${MINGW_PACKAGE_PREFIX}-${_realname}
         ${MINGW_PACKAGE_PREFIX}-${_realname}-tools)
pkgdesc="Provides LGPL Qt5 bindings for Python and related tools for binding generation (mingw-w64)"
pkgver=5.15.8
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://doc.qt.io/qtforpython-5/"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-qt5-declarative"
             "${MINGW_PACKAGE_PREFIX}-qt5-winextras"
             "${MINGW_PACKAGE_PREFIX}-qt5-xmlpatterns"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "${MINGW_PACKAGE_PREFIX}-qt5-multimedia"
             "${MINGW_PACKAGE_PREFIX}-qt5-location"
             "${MINGW_PACKAGE_PREFIX}-qt5-quickcontrols2"
             "${MINGW_PACKAGE_PREFIX}-qt5-remoteobjects"
             "${MINGW_PACKAGE_PREFIX}-qt5-scxml"
             "${MINGW_PACKAGE_PREFIX}-qt5-script"
             "${MINGW_PACKAGE_PREFIX}-qt5-sensors"
             "${MINGW_PACKAGE_PREFIX}-qt5-serialport"
             "${MINGW_PACKAGE_PREFIX}-qt5-speech"
             "${MINGW_PACKAGE_PREFIX}-qt5-charts"
             "${MINGW_PACKAGE_PREFIX}-qt5-svg"
             "${MINGW_PACKAGE_PREFIX}-qt5-datavis3d"
             "${MINGW_PACKAGE_PREFIX}-qt5-activeqt"
             "${MINGW_PACKAGE_PREFIX}-qt5-webchannel"
             "${MINGW_PACKAGE_PREFIX}-qt5-websockets"
             "${MINGW_PACKAGE_PREFIX}-qt5-3d"
             "${MINGW_PACKAGE_PREFIX}-qt5-gamepad")
_pkgfqn=pyside-setup-opensource-src-$pkgver
source=(https://download.qt.io/official_releases/QtForPython/pyside2/PySide2-${pkgver}-src/${_pkgfqn}.tar.xz
        001-pyside.patch
        002-shiboken.patch
        003-cmake-relative-paths.patch
        004-llvm-paths.patch
        005-Fix-building-against-numpy-1.23.patch
        006-limited-api.patch)
noextract=("${_pkgfqn}.tar.xz")
sha256sums=('23436302c8deb5b4cbc769b205d09604e38ba83b40708efccb7bd8c9af6f6b5d'
            '8120f1d29c4fe6928ab58d01586370af6d7c03f5a4c656d3e80ea03965a032d5'
            '8fe6965a942fd527d77d38c2a0b8e5c4f9bc1b51999f292af7a656b55a443713'
            '09c076b3fd61d8b972f05996e1383b6f31a695a8ae5db1aa6e0d9512d34db681'
            '75e720dc346d92167a6b6dde241f467ecdb1d1c4b9085843755b782f8c6c80a8'
            'c17c67f327c6e6ebbf2fd7c4c6038dd72d8fd147b9b75a7441a20794301500a6'
            'be6251b40e1f466e28509a5b5daad6b7bedbfa7120e400a6113cd56888020c22')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"
  tar -xJf ${srcdir}/${_pkgfqn}.tar.xz -C ${srcdir} || true

  cd "${srcdir}"/${_pkgfqn}

  apply_patch_with_msg \
    001-pyside.patch \
    002-shiboken.patch \
    003-cmake-relative-paths.patch \
    004-llvm-paths.patch \
    005-Fix-building-against-numpy-1.23.patch \
    006-limited-api.patch
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${_extra_config[@]} \
    -DBUILD_TESTS=OFF \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    ../${_pkgfqn}

  MSYS2_ARG_CONV_EXCL="--include-paths=;--typesystem-paths=" \
  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

package_shiboken2() {
  pkgdesc="CPython bindings generator for C++ libraries (mingw-w64)"
  provide=("${MINGW_PACKAGE_PREFIX}-shiboken2-qt5")
  conflicts=("${MINGW_PACKAGE_PREFIX}-shiboken2-qt5")
  replaces=("${MINGW_PACKAGE_PREFIX}-shiboken2-qt5")
  depends=("${MINGW_PACKAGE_PREFIX}-clang"
           "${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-qt5-xmlpatterns"
           "${MINGW_PACKAGE_PREFIX}-libxslt")

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}/sources/shiboken2

  local _PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${_f}"
  done

  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/Shiboken2-${pkgver}/*.cmake; do
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i "${_f}"
  done

  # Install egg-info
  cd "${srcdir}"/${_pkgfqn}
  ${MINGW_PREFIX}/bin/python setup.py egg_info --build-type=shiboken2
  _pythonpath=`${MINGW_PREFIX}/bin/python -c "from sysconfig import get_path; print(get_path('platlib'))"`
  cp -r shiboken2.egg-info "$pkgdir"/$(cygpath ${_pythonpath})
}

package_pyside2() {
  pkgdesc="Provides LGPL Qt5 bindings for Python and related tools for binding generation (mingw-w64)"
  provide=("${MINGW_PACKAGE_PREFIX}-pyside2-qt5")
  conflicts=("${MINGW_PACKAGE_PREFIX}-pyside2-qt5")
  replaces=("${MINGW_PACKAGE_PREFIX}-pyside2-qt5")
  depends=("${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-shiboken2"
           "${MINGW_PACKAGE_PREFIX}-qt5-declarative")
  optdepends=("${MINGW_PACKAGE_PREFIX}-qt5-winextras"
              "${MINGW_PACKAGE_PREFIX}-qt5-xmlpatterns"
              "${MINGW_PACKAGE_PREFIX}-qt5-tools"
              "${MINGW_PACKAGE_PREFIX}-qt5-multimedia"
              "${MINGW_PACKAGE_PREFIX}-qt5-location"
              "${MINGW_PACKAGE_PREFIX}-qt5-quickcontrols2"
              "${MINGW_PACKAGE_PREFIX}-qt5-remoteobjects"
              "${MINGW_PACKAGE_PREFIX}-qt5-scxml"
              "${MINGW_PACKAGE_PREFIX}-qt5-script"
              "${MINGW_PACKAGE_PREFIX}-qt5-sensors"
              "${MINGW_PACKAGE_PREFIX}-qt5-serialport"
              "${MINGW_PACKAGE_PREFIX}-qt5-speech"
              "${MINGW_PACKAGE_PREFIX}-qt5-charts"
              "${MINGW_PACKAGE_PREFIX}-qt5-svg"
              "${MINGW_PACKAGE_PREFIX}-qt5-datavis3d"
              "${MINGW_PACKAGE_PREFIX}-qt5-activeqt"
              "${MINGW_PACKAGE_PREFIX}-qt5-webchannel"
              "${MINGW_PACKAGE_PREFIX}-qt5-websockets"
              "${MINGW_PACKAGE_PREFIX}-qt5-3d")

  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}/sources/pyside2

  local _PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${_PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${_f}"
  done

  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/PySide2-${pkgver}/*.cmake; do
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i "${_f}"
  done

  # Install egg-info
  cd "${srcdir}"/${_pkgfqn}
  ${MINGW_PREFIX}/bin/python setup.py egg_info --build-type=pyside2
  _pythonpath=`${MINGW_PREFIX}/bin/python -c "from sysconfig import get_path; print(get_path('platlib'))"`
  cp -r pyside2.egg-info "$pkgdir"/$(cygpath ${_pythonpath})
}

package_pyside2-tools() {
  pkgdesc="PySide2 Tools (mingw-w64)"
  provide=("${MINGW_PACKAGE_PREFIX}-pyside2-tools-qt5")
  conflicts=("${MINGW_PACKAGE_PREFIX}-pyside2-tools-qt5")
  replaces=("${MINGW_PACKAGE_PREFIX}-pyside2-tools-qt5")
  depends=("${MINGW_PACKAGE_PREFIX}-pyside2")

  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake.exe --install build-${MSYSTEM}/sources/pyside2-tools

  rm -f ${pkgdir}${MINGW_PREFIX}/bin/{rcc,uic,designer}.exe  # provided by qt5-base
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
